package com.itheima.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.excption.MyException;
import com.itheima.pojo.SmsHomeBrand;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsHomeBrandService {
    @DubboReference
    private SmsHomeBrandApi smsHomeBrandApi;
    /**
     * 添加首页推荐品牌
     * @param homeBrandList
     * @return
     */
    public void create(List<SmsHomeBrand> homeBrandList) {
        smsHomeBrandApi.create(homeBrandList);
    }
    /**
     * 批量删除推荐品牌
     * @param ids
     * @return
     */
    public void deleteByIds(List<Integer> ids) {
        if(ObjectUtil.isEmpty(ids)){
            throw new MyException("选择一条记录");
        }
        smsHomeBrandApi.deleteByIds(ids);
    }

    /**
     * 分页查询推荐品牌
     * @param brandName
     * @param recommendStatus
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageResultVo list(String brandName, Integer recommendStatus, Integer pageNum, Integer pageSize) {
        IPage<SmsHomeBrand> iPage = smsHomeBrandApi.list(brandName, recommendStatus, pageNum, pageSize);
        List<SmsHomeBrand> smsHomeBrands=iPage.getRecords();
        PageResultVo resultVo=new PageResultVo();
        resultVo.setPageNum(pageNum);
        resultVo.setPageSize(pageSize);
        resultVo.setTotal(Convert.toInt(iPage.getTotal()));
        resultVo.setTotalPages(Convert.toInt(iPage.getPages()));
        resultVo.setList(smsHomeBrands);
        return resultVo;
    }

    /**
     * 批量修改推荐品牌状态
     * @param recommendStatus
     * @param ids
     * @return
     */
    public void updateStatus(Integer recommendStatus, List<Integer> ids) {
        smsHomeBrandApi.updateStatus(recommendStatus,ids);
    }

    /**
     * 修改推荐品牌排序
     * @param id
     * @param sort
     * @return
     */
    public void updateSortById(Integer id, Integer sort) {
        smsHomeBrandApi.updateSortById(id,sort);
    }
}
